/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Chiabai_total;

import java.awt.Color;
import java.awt.Image;
import java.awt.Point;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 *
 * @author Oho
 */
public class ThreadImages extends Thread {

    public static int LEFT = 4;
    public static int RIGHT = 2;
    public static int BUTOM = 1;
    public static int TOP = 3;
    RotatedJLabel lb;
    int x = 0, y = 0;
    float begin;
    float change;
    float destY = 1024;
    float time = 0;
    float start = 100;
    float duration = 100;
    float destX;
    int type;
    public static int MOVERIGHT = 6;
    public static int MOVEBUTTOM = 5;
    public static int MOVETOP = 7;
    public static int MOVELEFT = 8;
    
    public void InitBT(RotatedJLabel lb, int destX, int destY, int type) {
        this.destX = destX;
        this.destY = destY;
        this.lb = lb;
        begin = lb.getX();
        this.type = type;
        change = destX - begin;
        time = 10;
        duration = 1000;
        start = lb.getX();
        x = lb.getX();
        y = lb.getY();
    }

    public void InitLR(RotatedJLabel lb, int destX, int destY, int type) {
        this.destX = destX;
        //  this.destY =destY;
        this.lb = lb;
        begin = lb.getY();
        change = destY - begin;
        time = 10;
        duration = 1000;
        start = lb.getY();
        this.type = type;
        y = lb.getX();
    }
//
//    public void InitMoveLR(RotatedJLabel lb, int destX, int destY, int type) {
//        this.destX = destY;
//        //  this.destY =destY;
//        this.lb = lb;
//        begin = lb.getY();
//        change = destY - begin;
//        time = 10;
//        duration = 1000;
//        start = lb.getY();
//        this.type = type;
//        y = lb.getX();
//    }
//
//    public void InitMoveBT(RotatedJLabel lb, int destX, int destY, int type) {
//        this.destX = destX;
//        this.lb = lb;
//        begin = lb.getX();
//        this.type = type;
//        change = destX - begin;
//        time = 10;
//        duration = 1000;
//        start = lb.getX();
//        x = lb.getX();
//        y = lb.getY();
//    }

    public void Buttom() {
        try {
      
            int x1 = (int) LinearTweening.functioneaseInQuad(time, begin, change, duration);
            //              x=LinearTweening.funtionLinearTweening(x, 100, 220, 30) ;

            lb.setBounds(x1 + 200, y, 100, 100);
            //lb.setAlignmentX(x1);
            sleep(20);
            time += 10;
            y += 10;

            if (time == 250) {
                this.stop();
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadImages.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void MOVETOP() {
        try {
            int x1 = (int) LinearTweening.functioneaseInQuad(time, begin, change, duration);
            //              x=LinearTweening.funtionLinearTweening(x, 100, 220, 30) ;

            lb.setBounds(x1 , y, 100, 100);
            //lb.setAlignmentX(x1);
            sleep(20);
            time += 10;
            y -= 10;

            if (y>destY) {
                this.stop();
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadImages.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void Top() {
        try {
            int x1 = (int) LinearTweening.functioneaseInQuad(time, begin, change, duration);
            //              x=LinearTweening.funtionLinearTweening(x, 100, 220, 30) ;

            lb.setBounds(x1 + 200, y, 100, 100);
            //lb.setAlignmentX(x1);
            sleep(20);
            time += 10;
            y -= 10;

            if (time == 250) {
                this.stop();
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadImages.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public ThreadImages(RotatedJLabel lb, int destX, int destY, int type) {
        if (type == LEFT || type == RIGHT) {
            InitLR(lb, destX, destY - 4000, type);
        }
        if (type == TOP || type == BUTOM) {
            InitBT(lb, destX + 300, destY, type);
        }
        if (type == MOVEBUTTOM || type == MOVETOP) {
//            this.y = lb.getY();
            InitBT(lb, destX, destY, type);
        }
        if (type == MOVELEFT || type == MOVERIGHT) {
//            this.y = lb.getY();
            InitLR(lb, destX, destY , type);
        }


    }
    int i = 0;

    public void Left() {
        try {
            int x1 = (int) LinearTweening.functioneaseInQuad(time, begin, change, duration);
            lb.setBounds(y - 100, x1, 100, 100);
            sleep(20);
            time += 10;
            //y-=10;
            y = y - 10;
            if (time > 250) {
                this.stop();
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadImages.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void Right() {
        try {
            int x1 = (int) LinearTweening.functioneaseInQuad(time, begin, change, duration);
            lb.setBounds(y + 50, x1, 100, 100);
            sleep(20);
            time += 10;
            //y-=10;
            y = y + 10;
            if (time > 250) {
                this.stop();
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadImages.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
//duration goc
    public void MoveRight() {
        try {
           duration =200;
            int x1 = (int) LinearTweening.funtionLinearTweening(time, begin, change, duration);
            lb.setBounds(y, x1, 100, 100);
            sleep(20);
            time += 10;
            //y-=10;
            y = y - 10;
            if (y<=destX) {
                this.stop();
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadImages.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void MoveLeft() {
        try {
            duration =300;
            int x1 = (int) LinearTweening.funtionLinearTweening(time, begin, change, duration);
            lb.setBounds(y + 50, x1, 100, 100);
            sleep(20);
            time += 10;
            //y-=10;
            y = y + 10;
            if (y >= destX) {
                this.stop();
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadImages.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void MoveTop() {
        try {
                        duration =200;

            int x1 = (int) LinearTweening.funtionLinearTweening(time, begin, change, duration);
            lb.setBounds(x1, y, 100, 100);
            sleep(20);
            time += 10;
            //y-=10;
            y = y + 10;
            //sleep(100);
            if (y>=destY) {
                
                this.stop();
               
                    System.out.println("da thoat");
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadImages.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void MoveButtom() {
        try {
                        duration =200;

            int x1 = (int) LinearTweening.funtionLinearTweening(time, begin, change, duration);
            lb.setBounds(x1, y, 100, 100);
            sleep(20);
            time += 10;
            //y-=10;
            y = y - 10;
            //sleep(100);
            if (y <destY) {
                this.stop();
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadImages.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void run() {
        while (true) {
            switch (type) {
                case 1:
                    Buttom();
                    break;
                case 2:
                    Right();
                    break;
                case 3:
                    Top();
                    break;
                case 4:
                    Left();
                    break;
                case 5:
                    MoveButtom();
                    break;
                case 6:
                    MoveRight();
                    break;
                case 7:
                    MoveTop();
                    break;
                case 8:
                    MoveLeft();
                    break;
            }


        }
    }
}
